using ABA;
using JKFrame;
using System.Collections;
using UnityEngine;
using YooAsset;

//host clinet
public partial class Lanuch: MonoBehaviour
{
    void Start()
    {
        Application.targetFrameRate = 60;
        Init();

    }

    private IEnumerator OnHotUpdateSucceed()
    {
        Debug.Log("InitGlobal");
        ResSystemYooAsset.Instance.InstantiateGameObjectAsync("Global", null);
        yield return null;  
    }


    private void Init()
    {
        StartCoroutine(InitGame());
    }

    public IEnumerator InitGame()
    {
        yield return InitComm(); //YooAsset 
        yield return OnHotUpdateSucceed();
        //yield return LoadScene();
        //SysFirst.inst.Hide();

        //yield return FGUIPkg.inst.AddPackageCoroutine("a_text");
        //yield return FGUIPkg.inst.AddPackageCoroutine("login");

        //if (GameConf.LANGUAGE == "tc")
        //{
        //    string fileContent = ResSystemYooAsset.Instance.LoadAsset<TextAsset>("a_text_ppli5").text;
        //    FairyGUI.Utils.XML xml = new FairyGUI.Utils.XML(fileContent);
        //    UIPackage.SetStringsSource(xml);
        //    UIPackage.branch = "tc";
        //}

        //bool isLoadError = false;
        ////ResSystemYooAsset.Instance.LoadAssetAsync<Config_Error>(Config_Error.CONFIG_NAME, (p) => {
        ////    isLoadError = true;
        ////});
        //while (true)
        //{
        //    yield return null;
        //    if (isLoadError == true) break;
        //}

        ////Module.inst.RegisterAllModule();
        ////// Loader.inst.LoginGame(()=>{ 
        //////    EnterGame();
        //////    _isInit = true;   
        ////// });
        ////EnterGame();
        ////_isInit = true;

        //if (GameConf.IS_OPEN_GM == false)
        //{
        //    GameObject.Find("Reporter").SetActive(false);
        //}


    }

    public IEnumerator InitComm()
    {
#if UNITY_EDITOR
        GameConf.LS_PREFIX = "Editor" + GameConf.LS_PREFIX;
#endif
        CommData.DESIGN_WIDTH = GameConf.DESIGN_RESOLUTION_X;
        CommData.DESIGN_HEIGHT = GameConf.DESIGN_RESOLUTION_Y;
        CommData.PLAYER_PREFS_PREFIX = GameConf.LS_PREFIX;

#if UNITY_EDITOR// 手机上 Entry 会做初始化
        ResSystemYooAsset.Instance.Initialize();
        var initializationOperation = ResSystemYooAsset.Instance.InitPackageAsync(EPlayMode.EditorSimulateMode, CommData.YOOASSET_DEFAULT_PKG, EDefaultBuildPipeline.BuiltinBuildPipeline.ToString());
        yield return initializationOperation;

        var package = ResSystemYooAsset.Instance.GetPackage(CommData.YOOASSET_DEFAULT_PKG);
        var operation = package.RequestPackageVersionAsync();
        yield return operation;
        if (operation.Status != EOperationStatus.Succeed)
        {
            Debug.LogError(operation.Error);
        }
        else
        {
            Debug.Log($"Request package version : {operation.PackageVersion}");
        }
        var operation1 = package.UpdatePackageManifestAsync(operation.PackageVersion);
        yield return operation1;
        if (operation1.Status != EOperationStatus.Succeed)
        {
            Debug.LogError(operation1.Error);
        }

        var gamePackage = ResSystemYooAsset.Instance.GetPackage(CommData.YOOASSET_DEFAULT_PKG);
        ResSystemYooAsset.Instance.SetDefaultPackage(gamePackage);

        //yield return FGUIPkg.inst.AddPackageCoroutine(CommData.SYS_WIN_PKG_NAME);
        //FGUIManager.inst.InitUI();
#endif
        // 非编辑器的离线不热更模式
        if (GameConf.IS_OFFLINE_MODE && Application.isEditor == false)
        {
            ResSystemYooAsset.Instance.Initialize();
            // 打包出来的资源要拷贝到StreamingAssets/yoo/DefaultPackage/ 下面
            var initializationOperation_s = ResSystemYooAsset.Instance.InitPackageAsync(EPlayMode.OfflinePlayMode, CommData.YOOASSET_DEFAULT_PKG, EDefaultBuildPipeline.BuiltinBuildPipeline.ToString());
            yield return initializationOperation_s;

            var package_s = ResSystemYooAsset.Instance.GetPackage(CommData.YOOASSET_DEFAULT_PKG);
            var operation_s = package_s.RequestPackageVersionAsync();
            yield return operation_s;
            if (operation_s.Status != EOperationStatus.Succeed)
            {
                Debug.LogError(operation_s.Error);
            }
            else
            {
                Debug.Log($"Request package version : {operation_s.PackageVersion}");
            }
            var operation1_s = package_s.UpdatePackageManifestAsync(operation_s.PackageVersion);
            yield return operation1_s;
            if (operation1_s.Status != EOperationStatus.Succeed)
            {
                Debug.LogError(operation1_s.Error);
            }

            var gamePackage_s = ResSystemYooAsset.Instance.GetPackage(CommData.YOOASSET_DEFAULT_PKG);
            ResSystemYooAsset.Instance.SetDefaultPackage(gamePackage_s);

            // yield return FGUIPkg.inst.AddPackageCoroutine(CommData.SYS_WIN_PKG_NAME);
            //FGUIManager.inst.InitUI();
        }

        yield return null;
    }

    public IEnumerator LoadScene()
    {
        var handle = ResSystemYooAsset.Instance.LoadSceneAsync("GameScene");
        if (handle != null)
            yield return handle;
        yield return null;
    }

}
